package com.vfa.vocaburay;

import java.util.ArrayList;

import com.vfa.data.Category;
import com.vfa.data.CategoryData;
import com.vfa.data.DataHelper;
import com.vfa.data.toastMaking;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class InSertCategory extends Activity {
    private Button insertButton;
    private Button cancelButton;
    private EditText categotyText;
    private String text;
    private CategoryData category;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.insertcategory);
        insertButton = (Button) findViewById(R.id.insertButton);
        cancelButton = (Button) findViewById(R.id.cancelButton);
        categotyText = (EditText) findViewById(R.id.categorytext);
        category = new CategoryData(getApplicationContext());
        insertButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                insertCategory();
            }

        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                cancelInsert();
            }
        });
    }
    private void insertCategory() {
        // TODO Auto-generated method stub
        Log.e("LocNNT", "test insert ne ==========================");
        text = categotyText.getText().toString();
        Log.e("LocNNT", "test insert ne =========================="+text);
        if(!text.equals("".toString())&&!checkCategoryAvailable(text.trim())){
            category.insert( text);
        }
        else if(text.equals("")){
            toastMaking.errorToast(getApplicationContext(),  " ko chap nhan ten trong").show();
        }
        else{
             toastMaking.errorToast(getApplicationContext(),  text+ "    da ton tai trong database").show();
        }
        categotyText.setText("");
        
        text = "";
          ArrayList<Category>categoryName = new ArrayList<Category >();
          categoryName = category.getDatabase();
          
       for(int i=0;i<categoryName.size();i++){
           if(categoryName.get(i)!= null){
               Log.e("LocNNT", "test du lieus ne =========================="+categoryName.get(i).getCategoryName());
           }
       }
       Intent intent = new Intent(InSertCategory.this, CategoryActivity.class);
       startActivity(intent);
       InSertCategory.this.finish();
    }
    private void cancelInsert() {
        // TODO Auto-generated method stub
        Log.e("LocNNT", "test cancel ne ==========================");
        Intent intent = new Intent(InSertCategory.this, CategoryActivity.class);
        startActivity(intent);
        InSertCategory.this.finish();
    }
    private boolean checkCategoryAvailable(String name) {
        // TODO Auto-generated method stub
        ArrayList<Category >categoryName = new ArrayList<Category >();
        categoryName = category.getDatabase();
        for(int i=0;i<categoryName.size();i++){
             if(name.equals(categoryName.get(i).getCategoryName())){
                 return true;
             }
        }
        return false;
    }
}
